package com.bjpowernode.crm.web.controller;

import com.bjpowernode.crm.pojo.Dept;
import com.bjpowernode.crm.service.DeptService;
import com.bjpowernode.crm.utils.UUIDUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("dept")
public class DeptController {
    @Resource
    private DeptService deptService;


    //验证编号是否重复
    @RequestMapping("checkNo.do")
    @ResponseBody
    public int getCheckNo(String no){
        System.out.println(no);
        return deptService.getCheckNo(no);



    }

    //添加部门功能
    @RequestMapping("addDept.do")
    @ResponseBody
    public Map addDept(Dept dept){
        System.out.println("成功");
        //添加部门的id
        dept.setId(UUIDUtil.getUUID());
        deptService.addDept(dept);
        return new HashMap(){{
            put("success",true);
            put("msg","添加成功！");
        }};
    }


    //展示页面功能
    @RequestMapping("list.json")
    @ResponseBody
    public List<Dept> getAllDept(){
        return deptService.getAllDept();
    }

    //编辑数据回显
    @RequestMapping("editData.json")
    @ResponseBody
    public Dept getDept(String id){
        return deptService.getDept(id);
    }


    //编辑数据提交
    @RequestMapping("editDept.do")
    @ResponseBody
    public Map editDept(Dept dept){
        deptService.editDept(dept);
        return new HashMap(){{
            put("success",true);
            put("msg","添加成功！");
        }};
    }

    //删除功能
    @RequestMapping("delDept.do")
    @ResponseBody
    public Map delDept(String[] ids){
        deptService.delDept(ids);
        return new HashMap(){{
           put("success",true);
           put("msg","删除成功！");
        }};
    }
}
